package paleGoldenRod.client;

import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;
//import com.google.gwt.user.client.ui.Widget;

class BlindedPopup implements ClickListener
{
	PopupPanel popup;
	PopupPanel glass;
	ThinkMap tm;
	Button closeButton;
	
	public BlindedPopup(String message, boolean addDefaultStyles, ThinkMap tm)
	{
		this.tm = tm;
		/*******************************************************************
		 * A glass panel or 'blinder' to wash out the current screen
		 ******************************************************************/
		glass = new PopupPanel();
		glass.setStyleName("rx-glass");
		/*
		 * Set full screen
		 */
		DOM.setStyleAttribute(glass.getElement(), "width", "100%");
		DOM.setStyleAttribute(glass.getElement(), "height", "100%");
		/*
		 * Add default styles if required
		 */
		if (addDefaultStyles)
		{
			DOM.setStyleAttribute(glass.getElement(), "backgroundColor", "#000");
			DOM.setStyleAttribute(glass.getElement(), "opacity", "0.30");
			DOM.setStyleAttribute(glass.getElement(), "-moz-opacity", "0.30");
			DOM.setStyleAttribute(glass.getElement(), "filter", " alpha(opacity=30)");
		}
		/*******************************************************************
		 * A popup
		 ******************************************************************/
		popup = new PopupPanel(false);
		popup.setStyleName("rx-BlindedPopup");
		/*
		 * Add default styles if required
		 */
		if (addDefaultStyles)
		{
			DOM.setStyleAttribute(popup.getElement(), "backgroundColor", "#fff");
			DOM.setStyleAttribute(popup.getElement(), "border", "4px solid paleGoldenRod");
		}
		/*
		 * Popups can only hold one widget, so we need something to keep all
		 * our bits in
		 */
		VerticalPanel popupContents = new VerticalPanel();
		/*
		 * The header element
		 */
		Label header = new Label("Login with your ThinkMap Account");
		header.setStyleName("rx-BlindedPopup-header");
		if (addDefaultStyles)
		{
			DOM.setStyleAttribute(header.getElement(), "backgroundColor", "darkGreen");
			DOM.setStyleAttribute(header.getElement(), "borderBottom", "4px solid paleGoldenRod");
			DOM.setStyleAttribute(header.getElement(), "fontWeight", "bold");
			DOM.setStyleAttribute(header.getElement(), "fontSize", "90%");
			DOM.setStyleAttribute(header.getElement(), "padding", "5px");
			DOM.setStyleAttribute(header.getElement(), "textAlign", "center");
			DOM.setStyleAttribute(header.getElement(), "color", "white");

		}
		/*
		 * The main element
		 */
		HTML html = new HTML(message);
		html.setStyleName("rx-BlindedPopup-message");
		if (addDefaultStyles)
		{
			DOM.setStyleAttribute(html.getElement(), "fontSize", "80%");
			DOM.setStyleAttribute(html.getElement(), "padding", "15px");
			DOM.setStyleAttribute(html.getElement(), "textAlign", "right");

		}
		/*
		 * A button to close the popup
		 */
		closeButton = new Button("Login", this);
		DOM.setStyleAttribute(closeButton.getElement(), "width", "120px");
		DOM.setStyleAttribute(closeButton.getElement(), "padding", "5px");
		DOM.setStyleAttribute(closeButton.getElement(), "marginBottom", "10px");
		DOM.setStyleAttribute(closeButton.getElement(), "textSize", "14px");

		/*
		 * Assemble it all
		 */
		
		HorizontalPanel hp = new HorizontalPanel();
		
		DOM.setStyleAttribute(nickname.getElement(), "marginTop", "15px");
		//DOM.setStyleAttribute(nickname.getElement(), "margin", "15px");

		hp.add(html);
		hp.add(nickname);
		
		popupContents.add(header);
		popupContents.add(hp);
		//popupContents.add(html);
		//popupContents.add(nickname);
		popupContents.add(closeButton);
		popupContents.setCellHorizontalAlignment(closeButton, HasAlignment.ALIGN_CENTER);
		popup.add(popupContents);
		/*
		 * Show the glass first, then the popup will be over it
		 */
		glass.show();
		popup.center();
	}
	
	TextBox nickname = new TextBox();

	public void onClick(Widget sender)
	{
		closeButton.setHTML("<b>Loading...</b>");
	    Timer t = new Timer() {
		      public void run() {
		        init();
		      }
		    };
		    t.schedule(1);
	}
	public void init() {
		tm.msgNickname(nickname.getText());
		popup.hide();
		glass.hide();
	}
}